package com.franklin.ideaplugin.easytesting.ui;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.franklin.ideaplugin.api.ui.IBaseComponent;
import com.franklin.ideaplugin.api.ui.editor.CustomEditor;
import com.franklin.ideaplugin.api.ui.editor.FormatCustomJsonEditor;
import com.franklin.ideaplugin.api.utils.ProjectUtils;
import com.franklin.ideaplugin.api.utils.SwingUtils;
import com.franklin.ideaplugin.easytesting.common.entity.ETRsp;
import com.intellij.psi.PsiMethod;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author Ye Junhui
 * @since 2023/6/20
 */
public class ExecuteMethodResultForm implements IBaseComponent {
    private JPanel panel;
    private JPanel mainPanel;

    private final PsiMethod psiMethod;
    private final ETRsp<?> serverResponse;
    private CustomEditor editor;

    public ExecuteMethodResultForm(PsiMethod psiMethod, ETRsp<?> serverResponse) {
        this.psiMethod = psiMethod;
        this.serverResponse = serverResponse;
        this.init();
    }

    private void init(){
        if (Objects.nonNull(serverResponse)) {
            Object data = serverResponse.getData();
            if (serverResponse.getCode().equals("200")){
                String respJson = "{}";
                if (data instanceof Map || data instanceof Collection){
                    respJson = JSON.toJSONString(data);
                }else {
                    respJson = String.valueOf(data);
                }
                if (respJson.startsWith("\"")){
                    respJson = respJson.substring(1);
                }
                if (respJson.endsWith("\"")){
                    respJson = respJson.substring(0,respJson.length() - 1);
                }
                try {
                    //先尝试JSON数组
                    Object jsonObject = JSON.parse(respJson);
                    String prettyJson = JSON.toJSONString(jsonObject,JSONWriter.Feature.PrettyFormat);
                    editor = new CustomEditor(ProjectUtils.getCurrProject(), CustomEditor.JSON_FILE_TYPE);
                    editor.setText(prettyJson);
                } catch (Exception e) {
                    editor = new CustomEditor(ProjectUtils.getCurrProject(), CustomEditor.TEXT_FILE_TYPE);
                    editor.setText(respJson);
                }
            }else {
                editor = new CustomEditor(ProjectUtils.getCurrProject(),CustomEditor.TEXT_FILE_TYPE);
                editor.setText(String.valueOf(data));
            }
        }else {
            editor = new CustomEditor(ProjectUtils.getCurrProject(),CustomEditor.TEXT_FILE_TYPE);
            editor.setText("");
        }
        this.resize(new Dimension(500,700));
    }

    @Override
    public JComponent getMainPanel() {
        return this.mainPanel;
    }

    public void resize(Dimension dimension) {
        mainPanel.removeAll();
        mainPanel.setLayout(SwingUtils.createLayoutManager(1, 1));
        FormatCustomJsonEditor formatCustomJsonEditor = new FormatCustomJsonEditor(this.psiMethod.getName() + " Result", this.editor);
        this.addCell(mainPanel, formatCustomJsonEditor.getMainPanel(), 0, 0, dimension);
    }
}
